use serde::{Serialize, Deserialize};
use anyhow::Result;


#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
    // user_name: String,
    // password: String,
   pub user_id: String,
   pub device_id: String,
   pub access_token: String,
   pub homeserver_url: String,
   pub room_id: String,
   pub path: String,
   pub postgres_path: String,
   pub token: Option<String>,
    // session_id:
    // feeds: Vec<Feed>,
}
impl Config{
    pub fn new(user_id: String, device_id: String, access_token: String,homeserver_url: String, room_id: String, path:String, postgres_path: String) -> Self {
        Self { user_id, device_id, access_token, homeserver_url, room_id, path, /*feeds: Vec::new() ,*/ token: None, postgres_path}
    }
    // async fn add_feed(&mut self, feed: Feed) {
    //     self.feeds.push(feed);
    //     self.save_config().await;
    // }
    // async fn remove_feed(&mut self, url: String){
    //     for (i, feed) in self.feeds.clone().iter().enumerate() {
    //         if feed.url == url {
    //             self.feeds.remove(i);
    //         }
    //     }
    //     self.save_config().await;
    // }
    // fn feeds(&self) -> Vec<Feed> {
    //     self.feeds.clone()
    // }
    pub async fn load_config(path: &str) -> Result<Self> {
        let s = tokio::fs::read_to_string(path).await?;
        Ok(serde_json::from_str::<Self>(&s)?)
    }
    pub async fn save_config(&self) {
        let contents = serde_json::to_string_pretty(self).unwrap();
        tokio::fs::write(&self.path, contents).await.unwrap();
    }
}